﻿$(function () {
    $(window).scroll(function () {
        var top = $(window).scrollTop();
        var left = $(window).scrollLeft();
        $(".box").css({ left: left + "px", top: top + "px" });
        var parent = $("#mask").parent().attr("tagName");
        if (parent == "body") {
            $("#mask").css({ left: left + "px", top: top + "px" });
        }
    });
});



//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return decodeURI(r[2]); return null; //返回参数值 
}
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串 
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
//字符串过长显示...
function LengCheck(str, leng) {
    str = str == undefined ? "&nbsp;" : str;
    if (str.length > leng) {
        leng = leng - 1;
        return str.substr(0, leng) + "...";
    } else if (str == "") {
        leng = Math.abs(str.length - leng);
        for (var i = 0; i < leng; i++) {
             
            str = str + "&nbsp;";
        }
        return str;
    }
    return str;
}

//分页
function FenYe(pages, pagesize, liststep, count, docId, clickFun) {
    $("#" + docId).html("");
    $("#" + docId).append('<div style="height:28px;text-align: center"><ul class="pagination" id="paginationlayerGuest_' + docId + '" style="margin:0 0 0 15px;text"></ul></div>');

    //pagesize 每页显示记录数
    //liststep 最多显示分页页数
    //pages 默认显示第一页
    //count 记录总数
    var pagescount = count % pagesize == 0 ? (count / pagesize) : parseInt(count / pagesize + 1);//求总页数
    if (pagescount < pages) {
        pages = pagescount;//如果分页变量大总页数，则将分页变量设计为总页数
    }
    if (pages < 1) {
        pages = 1;//如果分页变量小于１,则将分页变量设为１
    }
    // 从第几页开始显示分页信息
    var listbegin = pages - (liststep % 2 == 0 ? (liststep / 2) : parseInt(liststep / 2 + 1));
    if (listbegin < 1) {
        listbegin = 1;
    }
    var listend = listbegin + liststep;//分页信息显示到第几页
    if (listend > pagescount) {
        listend = pagescount + 1;
    }

    //显示数据部分
    var recordbegin = (pages - 1) * pagesize;//起始记录
    var recordend = 0;
    recordend = recordbegin + pagesize;

    if (pages == pagescount && count % pagesize != 0) {
        recordend = parseInt(recordbegin + (count % pagesize));
    }

    $("#" + "paginationlayerGuest_" + docId).html("");
    //<显示上一页
    if (pages > 1) {
        $("#" + "paginationlayerGuest_" + docId).append("<li><a href='###' onclick='" + clickFun + "(" + (pages - 1) + ")'>&laquo;</a></li>");
    }
    //<显示分页码
    for (var i = listbegin; i < listend; i++) {
        if (i != pages) {//如果i不等于当前页
            $("#" + "paginationlayerGuest_" + docId).append("<li><a href='###' onclick='" + clickFun + "(" + i + ")'>" + i + "</a></li>");
        } else {
            $("#" + "paginationlayerGuest_" + docId).append("<li><a href='###' style='background-color:#DDDDDD'onclick='" + clickFun + "(" + i + ")'>" + i + "</a></li>");
        }
    }
    //<显示下一页
    if (pages != pagescount && pagescount != 0) {
        $("#" + "paginationlayerGuest_" + docId).append("<li><a href='###'onclick='" + clickFun + "(" + (pages + 1) + ")'>&raquo;</a></li>");
    }
}
//建立一个可存取到file的url
function getObjectURL(file) {
    if (file == undefined) {
        return null;
    }
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}
//读秒按钮
function BtnTimer(id, second, txtHead, txtDown) {
    var txt = $('#' + id).html();
    $('#' + id).attr("disabled", true);
    var countdown = setInterval(CountDown, 1000);
    function CountDown() {
        $('#' + id).html(txtHead + second + txtDown);
        if (second == 0) {
            $('#' + id).html(txt).removeAttr("disabled");
            clearInterval(countdown);
        }
        second--;
    }


}

//加载动画
function layerLoad(id) {
    var text = '<div id="mask" class="mask"><div class="spiner-example"><div class="sk-spinner sk-spinner-fading-circle"><div class="sk-circle1 sk-circle"></div><div class="sk-circle2 sk-circle"></div><div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div><div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div></div></div>';
    var parent = $("#" + id);
    if (parent.length == 0) {
        parent = $(".layui-layer-content");
    }
    if (parent.length == 0) {
        parent = $("body");
    }
    parent.append(text);
    $("#mask").show();
}
//关闭加载动画
function layerLoadClose() {
    $("#mask").remove();
}
//补位 前
function CoverFirst(str, leng) {
    if (str.length < leng) {
        leng = str.length - leng;
        for (var i = 0; i < leng; i++) {
            //全角空格
            str = "　" + str;
        }
        return str;
    }
    return str == undefined ? "" : str;
}
//补位  后
function CoverLast(str, leng) {
    if (str.length < leng) {
        leng = leng - str.length;
        for (var i = 0; i < leng; i++) {
            //全角空格
            str += "　";
        }
        return str;
    }
    return str == undefined ? "" : str;
}
function Colro(val) {
    switch (val) {
        case "1":
            return "color-green";
        case "2":
            return "color-gray";
        case "3":
            return "color-ju";
        default:
            return "";
    }
}
function MaxPhoto(obj, newUrl) {
    var ImgList = $(obj).parent().parent().find("img");

    if ($(".box").length == 0) {
        $("body").append('<div class="box"><div class="boxK"><a class="close" id="box_close">×</a> <a class="left-btn" id="box_left">‹</a> <a class="right-btn" id="box_right">›</a> </div > <div class="img" id="box_Img"> </div> </div ></div>');
        var top = $(window).scrollTop();
        var left = $(window).scrollLeft();
        $(".box").css({ left: left + "px", top: top + "px" });
        var parent = $("#mask").parent().attr("tagName");
        if (parent == "body") {
            $("#mask").css({ left: left + "px", top: top + "px" });
        }
    }
    var leng = ImgList.length;
    var index = 0;
    for (var i = 0; i < leng; i++) {
        if ($(obj).attr("id") == $(ImgList[i]).attr("id")) {
            index = i;
            break;
        }
    }

    for (var i = 0; i < leng; i++) {
        if (i == index) {
            $("#box_Img").append("<img src='" + $(ImgList[i]).attr("src") + "' class='' />");
        }
        else if (index > i) {
            $("#box_Img").append("<img src='" + $(ImgList[i]).attr("src") + "'style='margin-left: -2450px;' />");
        } else {
            $("#box_Img").append("<img src='" + $(ImgList[i]).attr("src") + "'style='margin-left:2450px;' />");
        }
    }


    $("#box_close").click(function () {
        $(".box").remove();
    });
    var margin = $("#box_Img > img").eq(index).css("margin");
    $("#box_left").click(function () {
        $("#box_Img > img").eq(index).animate({ marginLeft: "2450px" }, 600);
        index--;
        if (index < 0) {
            index = leng - 1;
        }
        $("#box_Img > img").eq(index).css("margin-left", "-2450px");
        $("#box_Img > img").eq(index).animate({ margin: margin }, 600);

    });
    $("#box_right").click(function () {
        $("#box_Img > img").eq(index).animate({ marginLeft: "-2450px" }, 600);
        index++;
        if (index == leng) {
            index = 0;
        }
        $("#box_Img > img").eq(index).css("margin-left", "2450px");
        $("#box_Img > img").eq(index).animate({ margin: margin }, 600);
    });

    $(".box").show();
}

function Encode(str) {
    if (str == "" || str == undefined) {
        return "";
    }
    str = str.replace(/[\r\n]/g, "<br/>")//
    str = str.replace(/[\n]/g, "<br/>");

    return str;
}
//字符串中的 \r \n   \r\n  替换成<br/>
String.prototype.formatEncode = function (str, str2) {
    var str = this;
    if (str == "" || str == undefined) {

        return str2 == undefined ? "暂无数据" : str2;
    }
    str = str.replace(/[\r\n]/g, "<br/>")//
    str = str.replace(/[\n]/g, "<br/>"); 
    return str;
}
//时间日期格式化
Date.prototype.format = function (format) {
    /* 
     * 使用例子:format="yyyy-MM-dd hh:mm:ss"; 
     */
    var o = {
        "M+": this.getMonth() + 1, // month  
        "d+": this.getDate(), // day  
        "h+": this.getHours(), // hour  
        "m+": this.getMinutes(), // minute  
        "s+": this.getSeconds(), // second  
        "q+": Math.floor((this.getMonth() + 3) / 3), // quarter  
        "S": this.getMilliseconds()
        // millisecond  
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4
            - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1
                ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}